/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.reportes;

import com.uca.sisbec.beans.NameAndCarnetBean;
import com.uca.sisbec.beans.ReporteGastosBean;
import com.uca.sisbec.beans.ReporteGastosXBecBean;
import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.reportes.ReporteDeGastosForm;
import com.uca.sisbec.helpers.reportes.ReporteGastosHelper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

/**
 *
 * @author Alex
 */
public class ReporteGastosAction extends HibernateAction {

    public ActionForward init(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ReporteDeGastosForm rpgf = (ReporteDeGastosForm) form;
        rpgf.reset(mapping, request);
        return mapping.findForward("success");
    }

    public ActionForward ConstruirReporteDeGastos(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {

        ReporteDeGastosForm rpgf = (ReporteDeGastosForm) form;
        ActionMessages msg = new ActionMessages();
        ActionErrors errors = new ActionErrors();
        ReporteGastosHelper rpgh = new ReporteGastosHelper(getSession(request));
        
        if(rpgf.getFechaIni().trim().equals("") || rpgf.getFechaFin().trim().equals("")){
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("fechas.vacias"));
            request.setAttribute("errors", errors);
        } else {
            SimpleDateFormat formatoDeFecha = new SimpleDateFormat("yyyy-mm-dd");
            Date fechaIni = formatoDeFecha.parse(rpgf.getFechaIni());
            Date fechaFin = formatoDeFecha.parse(rpgf.getFechaFin());

            if (fechaFin.before(fechaIni) == true) {
                errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("intervalfecha.invalido"));
                request.setAttribute("errors", errors);
            } else {
                List<ReporteGastosBean> reporte = rpgh.ConstruirReporteGastos(rpgf);
                
                String beneficiados = "";
                
                for (int i = 0; i < reporte.size(); i++) {
                    List<NameAndCarnetBean> listaBecs = rpgh.ListaBecariosBenef(reporte.get(i).getIdGasto());
                    for (int j = 0; j < listaBecs.size(); j++) {
                        beneficiados = beneficiados + listaBecs.get(j).getNameAndCarnet() + "<br />";
                    }
                    //beneficiados = beneficiados.substring(0, beneficiados.length() - 2);
                    reporte.get(i).setBeneficiados(beneficiados);
                    beneficiados = "";
                }
                
                msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("reporte.creado"));
                request.setAttribute("msg", msg);
                request.setAttribute("reporte", reporte);
            }
        }
        request.setAttribute("tipog", rpgf.getTipoGasto());
        return mapping.findForward("success");
    }
    
    public ActionForward Recibo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        //ReporteDeGastosForm rpgf = (ReporteDeGastosForm) form;
        //request.setAttribute("monto", rpgf.getMonto());
        
        return mapping.findForward("recibo");
    }
    
}
